package com.fc.example.adapter;

import android.app.Activity;
import android.content.Context;

import com.anythink.core.api.ATAdConst;
import com.anythink.core.api.ATBiddingListener;
import com.anythink.core.api.ATBiddingResult;
import com.anythink.interstitial.unitgroup.api.CustomInterstitialAdapter;
import com.fc.example.CustomApplication;
import com.yfanads.android.core.inter.YFAdInterstitialAds;
import com.yfanads.android.core.inter.YFInterstitialListener;
import com.yfanads.android.model.YFAdError;
import com.yfanads.android.utils.YFLog;
import com.yfanads.android.utils.YFUtil;

import java.util.Map;
import java.util.UUID;

/**
 * 一句话描述该类功能.
 *
 * @author JamesQian
 * @version 1.0
 * @copyright 亿帆
 * @date 2024/1/8 16:05
 **/
public class YFInterAdapter extends CustomInterstitialAdapter {
    String mUnitid;
    String appid;
    final static String TAG = "YFInterAdapter";
    private YFAdInterstitialAds easyInterstitial;


    @Override
    public void show(Activity activity) {
        if (easyInterstitial != null) {
            easyInterstitial.showAds(activity);
        }
        YFLog.debug(TAG + " show");
    }

    @Override
    public boolean startBiddingRequest(Context context, Map<String, Object> serverExtras, Map<String, Object> localExtra, ATBiddingListener biddingListener) {
        if (serverExtras.containsKey("appid") && serverExtras.containsKey("unitid")) {
            appid = (String) serverExtras.get("appid");
            mUnitid = (String) serverExtras.get("unitid");
            YFUtil.switchMainThread(() -> startLoadAds(context, biddingListener));
        } else {
            if (mLoadListener != null) {
                mLoadListener.onAdLoadError("", "appid or unitid is empty!");
            }
        }
        YFLog.info(TAG + " startBiddingRequest appid_" + appid + ",mUnitid_" + mUnitid);
        return true;
    }

    @Override
    public void loadCustomNetworkAd(Context context, Map<String, Object> serverExtras, Map<String, Object> localExtras) {
        if (serverExtras.containsKey("appid") && serverExtras.containsKey("unitid")) {
            appid = (String) serverExtras.get("appid");
            mUnitid = (String) serverExtras.get("unitid");
            YFUtil.switchMainThread(() -> startLoadAds(context, null));
        } else {
            if (mLoadListener != null) {
                mLoadListener.onAdLoadError("", "appid or unitid is empty!");
            }
        }
        YFLog.info(TAG + " loadCustomNetworkAd appid_" + appid + ",mUnitid_" + mUnitid);
    }

    private void startLoadAds(Context context, ATBiddingListener biddingListener) {
        YFInit.init(CustomApplication.getInstance(), appid);
        releaseAd();
        easyInterstitial = new YFAdInterstitialAds(context, new YFInterstitialListener() {
            @Override
            public void onAdSuccess() {
                YFLog.debug(TAG + " onAdSuccess");
                if (biddingListener != null) {
                    double ecpm = easyInterstitial.getEcpm();
                    if (ecpm < 0) {
                        ecpm = 0;
                    }
                    //获取货币单位
                    ATAdConst.CURRENCY currency = ATAdConst.CURRENCY.RMB_CENT;
                    //用于标识此次竞价的唯一ID，可使用UUID或者时间戳
                    String token = UUID.randomUUID().toString();
                    biddingListener.onC2SBiddingResultWithCache(ATBiddingResult.success(ecpm, token, null, currency), null);
                } else {
                    if (mLoadListener != null) {
                        mLoadListener.onAdCacheLoaded();
                    }
                }
            }

            @Override
            public void onAdExposure() {
                YFLog.debug(TAG + " onAdExposure");
                if (mImpressListener != null) {
                    mImpressListener.onInterstitialAdShow();
                }
            }

            @Override
            public void onAdClicked() {
                YFLog.debug(TAG + " onAdClicked");
                if (mImpressListener != null) {
                    mImpressListener.onInterstitialAdClicked();
                }
            }

            @Override
            public void onAdClosed() {
                YFLog.debug(TAG + " onAdClosed");
                if (mImpressListener != null) {
                    mImpressListener.onInterstitialAdClose();
                }
            }

            @Override
            public void onAdFailed(YFAdError yfAdError) {
                YFLog.debug(TAG + " onAdFailed " + yfAdError.toString());
                if (biddingListener != null) {
                    biddingListener.onC2SBiddingResultWithCache(ATBiddingResult.fail(yfAdError.msg), null);
                } else if (mLoadListener != null) {
                    mLoadListener.onAdLoadError(yfAdError.code, yfAdError.msg);
                }
            }

            @Override
            public void onAdRenderFailed() {
                YFLog.debug(TAG + " onAdRenderFailed");
            }
        });
        easyInterstitial.loadOnly(mUnitid);
    }

    @Override
    public void destory() {
        YFLog.debug(TAG + " destroy");
        releaseAd();
    }

    private void releaseAd() {
        try {
            if (easyInterstitial != null) {
                easyInterstitial.destroy();
                easyInterstitial = null;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /***
     * 提供本次加载自定义广告平台的广告位id.
     **/
    @Override
    public String getNetworkPlacementId() {
        return mUnitid;
    }

    /***
     * getNetworkSDKVersion.
     **/
    @Override
    public String getNetworkSDKVersion() {
        return YFInit.sdkVersion;
    }

    @Override
    public String getNetworkName() {
        return YFInit.networkName;
    }

    @Override
    public boolean isAdReady() {
        return easyInterstitial != null && easyInterstitial.isReady();
    }

}
